<?php
/*
 * Created on Jul 2, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 $loader->requireOnce('includes/emrCore/EmrClassNotFoundException.class.php');
 $loader->requireOnce('includes/emrCore/EmrMethodNotFoundException.class.php');
 $loader->requireOnce('includes/emrCore/EmrEmptyException.class.php');
 $loader->requireOnce('includes/emrCore/ISelectionPanelBuilder.class.php');
 $loader->requireOnce('ordo/ListOfMedicalRecordItem.class.php');
 
 
 class GeneralEmrProcessor {
 	/**
 	 * 
 	 * @var Controller
 	 */
 	private $controller = null;
 	
 	/**
 	 * 
 	 * @var ISelectionPanelBuilder
 	 */
 	private $panelBuilder = null; 	 	

 	private $logger;
 	 	
 	private $listOfDepartment;
 	private $listOfEncounterType;
 	private $currentDepartmentId;
 	private $currentEncounterTypeId;
 	 	
 	public function GeneralEmrProcessor($controller) {
 		$this->controller = $controller;
 		$this->logger = LoggerManager::getLogger('core.includes.coreEmr.GeneralEmrProcessor');
 	}

 	public function setPanelBuilder($panelBuilder) {
 		if ($panelBuilder instanceof ISelectionPanelBuilder) {
 			$this->panelBuilder = $panelBuilder;
 		}
 	}

 	/**
 	 * Build the selection panel.
 	 * 
 	 * @param $recordId int medical record id
 	 * @return HTML
 	 * 
 	 * @throws EmrCoreException
 	 */
 	public function buildSelectionPanel($recordId) {  
 		if (!($this->panelBuilder instanceof ISelectionPanelBuilder)) { 			 			
 			throw new EmrCoreException('panelBuilder type checking error');	 				
 		}		 	 		
 		
 		if ($this->controller == null) {
 			throw new EmrCoreException('Null controller in EMR processor');
 		}
 		
 		return $this->panelBuilder->buildSelectionPanel(
 										$this->controller,
 										$recordId,
 										$this->listOfDepartment,
 										$this->listOfEncounterType,
 										$this->currentDepartmentId,
 										$this->currentEncounterTypeId);	 		 				
 	} 	
 	
 	/**
 	 * Load an medical record dynamically.
 	 * 
 	 * @param $emrId int encounter_type_id
 	 * @param $parameters array parameter list 
 	 * @return HTML in normal case or throw exceptions
 	 *  
 	 * @throws 
 	 * 			EmrClassNotFoundException <br />
 	 * 			EmrMethodNotFoundException
 	 */
 	public function loadEmr($emrId, $parameters = array()) {
 		$mapObj = Celini::newORDO("ListOfMedicalRecordItem"); 
 		$requestedEmr = $mapObj->getEmrById($emrId); 		
 		$emrClassName ='';
 		$emrMethodName ='';
 		if ($requestedEmr != null) {
 			$emrClassName = $requestedEmr['emr_class_name'];
 			$emrMethodName = $requestedEmr['emr_method_name'];
 			
 		}
 		return Utils::invokeEmr($emrClassName,$emrMethodName,$parameters);
 	}
 	
 	/**
 	 * Initialize the processor.
 	 * 
 	 * Process getting department and encounter type
 	 * 
 	 * @param $selectedDepId int current selected department id. Could be null
 	 * @param $selectedEmrId int current selected encounter type id. Could be null
 	 * 
 	 * @throws EmrEmptyException
 	 */
 	public function initialProcessor($selectedDepId, $selectedEmrId) {
 	/* case Except: map empty -> show error.
 		 * case 1: currDep != null > 0 and currDep in map
 		 *  case 1.1  $selectedEmr != null > 0 in map
 		 * 		-> build map + load emr -> set selection = (currDep, selectedEmr)
 		 *  case 1.2  $selectedEmr == null ==0 or not in map
 		 * 		-> build map + load default emr for currDep 
 		 * 			-> set selelection= (currDep, defaultEmr)
 		 *  
 		 * 
 		 * case 2 currDep != null > 0 and currDep not in map
 		 * 		-> build map + load the first one in map -> set selection(item.dep,item.erm)
 		 * case 3 currDep == null or 0
 		 *  case 3.2: selectedEmr == null or 0 or not in map
 		 *  	-> build map + load the first one in map ->  set selection(item.dep,item.erm)
 		 *  case 3.1: selectedEmr != null and in map
 		 * 		-> build map + load the first map element contain selectedEmr
 		 *   	 	->set selection(item.dep,item.erm)
 		 *  
 		*/		
 		
 		$depId = $selectedDepId;
 		$emrId = $selectedEmrId;
 		//case Except	
 		$mapObj = Celini::newORDO("ListOfMedicalRecordItem"); 		
 		$listOfDep = $mapObj->listOfMappedDepartments();
 		if ($listOfDep == null) { 			 			
 			throw new EmrEmptyException('There is no medical record detail'); 			
 		}  
 		
 		$listOfItems = $mapObj->listOfMedicalRecordItems();
 		
 		if ($listOfItems == null) { 
 						
 			throw new EmrEmptyException('There is no medical record detail'); 			
 		}	
 		

 		if ($selectedDepId != null 
 			&& $selectedDepId > 0
 			&& $mapObj->isDepInMap($selectedDepId) == true) {
 			//case 1
 			if ($selectedEmrId != null && $selectedEmrId > 0
 				&& $mapObj->isEmrInMap($selectedEmrId) == true) {
 				//case 1.1
 				//-> build map + load emr(currDep, selectedEmr) 
 				//-> set selection = (currDep, selectedEmr)
 				$depId = $selectedDepId;
 				$emrId = $selectedEmrId;
 			} else {
 				//case 1.2
 				//-> build map + load default emr for currDep 
 		  		//	-> set selelection= (currDep, defaultEmr)
 		  		$depId = $selectedDepId;
 				$emrId = $mapObj->getDefaultEmr($depId);
 				if ($emrId == null) {
 					$item = $this->getFirstItemContainDep($listOfItems, $depId);
 					if ($item != null) {
 						//the first on contain depid
 						$depId = $item['department_id'];
	 					$emrId = $item['emr_id'];
 					} else {
 						//the first one in list
 						$depId = $listOfItems[0]['department_id'];
 						$emrId = $listOfItems[0]['emr_id'];
 					 	
 					}		
	 		    	
 				}
 				
 			}
 				
 		} else if ($selectedDepId != null 		 
 			&& $selectedDepId > 0
 			&& $mapObj->isDepInMap($selectedDepId) == false) {
 			//case 2
 			//-> build map + load the first one in map -> 
 			//set selection(item.dep,item.erm)
 			$depId = $listOfItems[0]['department_id'];
 			$emrId = $listOfItems[0]['emr_id'];
 				
 		
 		} else  {
 			//case 3
 			if ($selectedEmrId != null && $mapObj->isEmrInMap($selectedEmrId) == true) {
 				//case 3.1
 				//-> build map + load the first map element contain selectedEmr
 		    	// 	->set selection(item.dep,item.erm)
 		    	$item = $this->getFirstItemContainEmr($listOfItems,$selectedEmrId);
 		    	if ($item != null) {
 						//the first on contain emr id
 						$depId = $item['department_id'];
	 					$emrId = $item['emr_id'];
 				} else {
 						//the first one in list
 						$depId = $listOfItems[0]['department_id'];
 						$emrId = $listOfItems[0]['emr_id'];
 					 	
 				}
 		    	
 			} else {
 				//case 3.2
 				//-> build map + load the first one in map 
 				//->  set selection(item.dep,item.erm)
 				$depId = $listOfItems[0]['department_id'];
 				$emrId = $listOfItems[0]['emr_id']; 				
 			}	
 		}
 		
 		
 		$this->currentDepartmentId = $depId;
 		$this->currentEncounterTypeId = $emrId;
 		$this->listOfDepartment = $listOfDep;
 		$this->listOfEncounterType = $listOfItems;
 	}
 	
 	
  	/**
 	 * Get the first item contain emrId in listOfItems.
 	 * @return if found : item('department_id','emr_id','emr_name')
 	 * if not found : null
 	 */
 	private function getFirstItemContainEmr($listOfItems, $emrId) {
 		if (!isset($listOfItems) || !isset($emrId)) {
 			return null;
 		} 		
 		for ($i = 0; $i < count($listOfItems); $i++) {
 			if ($emrId == $listOfItems[$i]['emr_id']) {
 				return $listOfItems[$i];
 			} 			
 		} 		 		
 		return null;
 	}
 	
 	
 	/**
 	 * Get the first item contain depid in listOfItems
 	 * @return if found : item('department_id','emr_id','emr_name')
 	 * if not found : null
 	 */
 	private function getFirstItemContainDep($listOfItems, $depId) {
 		if (!isset($listOfItems) || !isset($depId)) {
 			return null;
 		} 		
 		for ($i = 0; $i < count($listOfItems); $i++) {
 			if ($depId == $listOfItems[$i]['department_id']) {
 				return $listOfItems[$i];
 			} 			
 		} 		 		
 		return null;
 	}
 }
?>
